package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.CaringService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/caring")
public class CaringController {
    @Autowired
    CaringService caringService;

    /**
     * 客户关怀分页查询
     * @param pageNum
     * @param pageSize
     * @param search
     * @param type
     * @return
     */
    @RequestMapping("/caring-list")
    @ResponseBody
    public Map<String,Object> selectCaring(Integer pageNum,Integer pageSize,String search,String type, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        caringService.selectCaring(search,type,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }


    /**
     * 通过id查找需要修改的客户关怀
     * @param caringId
     * @return
     */
    @RequestMapping("/selectId")
    @ResponseBody
    public List<Caring> selectId(Integer caringId){
        return caringService.selectId(caringId);
    }

    /**
     * 关怀类型列表
     * @return
     */
    @RequestMapping("/type-list")
    @ResponseBody
    public List<CaringType> selectType(){
        return caringService.selectType();
    }

    /**
     * 新增(修改)客户关怀
     * @param caring
     */
    @RequestMapping("/addCaring")
    @ResponseBody
    public void addCaring(@RequestBody Caring caring){
        System.out.println("关怀"+caring);
        caringService.addCaring(caring);
    }

    /**
     * 删除客户关怀
     * @param caringId
     */
    @RequestMapping("/delCaring")
    @ResponseBody
    public void delCaring(Integer caringId){
        caringService.delCaring(caringId);
    }

    /**
     * 客户关怀客户列表
     * @param khsearch
     * @return
     */
    @RequestMapping("/kh-list")
    @ResponseBody
    public Map<String,Object> selectKh(String khsearch,Integer pageNum,Integer pageSize,String empName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        caringService.selectKh(khsearch,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 客户联系人列表
     * @param khName
     * @return
     */
    @RequestMapping("/lxr-list")
    @ResponseBody
    public List<Contacts> selectLxr(String khName){
        return caringService.selectLxr(khName);
    }

}
